* Set log
cap log close
log using "${LOG}/2_datamerge_soepregio.log", replace

/*
#-----------------------------------#
# Replication of:
# Seeking Opportunity in the Knowledge Economy: Moving Places, Moving Politics?
# Valentina Consiglio, Thomas Kurer

# File: 2_data_soepregio
# Note: This file merges all relevant variables from the restricted-use version
# of the SOEP.

#-----------------------------------#
*/

*****************************
** 1 | Load and Merge Data **
****************************

* Use Person-Related Metadata from ppathl 
use cid pid hid syear erstbefr letztbef prgroup psample netto phrf phrf1 pbleib gebjahr sex partner migback pop    		using "${IN_LONG}/ppathl.dta", clear

* Merge information from hpathl
merge m:1 hid syear using "${IN_LONG}/hpathl.dta", keep(1 3) nogen ///
	keepus (hhrf hhrf1 hbleib hnetto hpop sampreg hrgroup hsample)
	

* Merge information from pl 
merge 1:1 pid syear using "${IN_LONG}/pl.dta", keep(1 3) nogen ///
		keepus (ple0010_h pla0009_v2 pla0009_v3 pld0131_v1 pld0131_v2 plh0333 plh0004 plh0007 pli0097_h 				pli0097_v1 pli0097_v2 pli0097_v3 plh0397i04 plh0397i03 plh0397i01 plh0397i02 plh0151 					plh0150 plh0149 plh0152_v2 plh0177 plh0156 plj0681 plj0687 plj0686 plj0454 plh0173 						plh0176 plh0175 plh0179 plh0162 plh0347 plh0350 plh0178 plh0181 plh0187 plh0186 						plh0042 plh0040 plh0280 plh0032 plh0033 plh0029 plh0336 plh0030 plh0279 plh0409 							plh0408 plj0600 plh0028 plh0244 plh0337_v1 plh0337_v2 plh0011_h plh0012_h 								plh0013_h plh0006 pli0096_h pli0090_h pli0091_h pli0092_h pli0093_h pli0094_h pli0095_h pli0098_h)

* Merge information from hl 
merge m:1 hid syear using "${IN_LONG}/hl.dta", keep(1 3) nogen ///
	keepus (hlf0107_h hlf0108_h hlf0126 hlf0127 hlf0128 hlf0129 hlf0130 hlf0131 hlf0132)
	
* Merge information from bioparen 
merge m:1 pid using "${IN_LONG}/bioparen.dta", keep(1 3) nogen ///
	keepus (fsedu msedu fprofedu mprofedu fkldb10 mkldb10 fisei08 misei08 fmps08 mmps08 fegp08 megp08 				fisco08 misco08)

* Merge information from movedist 
merge 1:1 pid syear using "${IN_LONG}/movedist.dta", keep(1 3) nogen ///
	keepus (resmove chg_kkz chg_zip distance)	
	
* Merge information from pgen 
merge 1:1 pid syear using "${IN_LONG}/pgen.dta", keep(1 3) nogen ///
	keepus(pgfamstd pgisced11 pgisced97 pgpsbil pgnation pglabgro pglabnet pgsndjob pgstib pgemplst pgegp08 pgegp88 				pgerljob pgausb pglfs pgisco08 pgisco88 pgpbbil01 pgpbbil02 pgpbbil03 pgtatzeit pgvebzeit pgexpue 					pgexpft pgexppt pgisced11 pgisei88 pgisei08)
	
* Merge information from hgen 
merge m:1 hid syear using "${IN_LONG}/hgen.dta", keep(1 3) nogen ///
	keepus(hgtyp1hh hghinc hgi1hinc) 
	
* Merge information from pequiv 
merge 1:1 pid syear using "${IN_LONG}/pequiv.dta", keep(1 3) nogen ///
	keepus(i11102 i11103 d11107 d11106 d11104 h11103 h11104 h11105 h11106 h11107 h11108 h11109 h11110 l11101) 

* Merge information from regio file
* (!!) to be changed on regio hub computer
merge m:1 hid syear using "${IN_LONG}/regionl.dta", keep(1 3) nogen ///
	keepus(bula regbez ror96 plz kkz_rek nuts3 nuts2 nuts1 kr_utmost kr_utmnord kr_area kr_population 				kr_uemprate kr_foreigner kr_emprate kr_popdens kr_hhinc kr_gdp_pem kr_gdp_pc)


*************************
** 2 | Recode Missings **
*************************

* No answer
mvdecode _all, mv(-1=.a)

* Not applicapble
mvdecode _all, mv(-2=.b)

* Answer imporbable or multiple response
mvdecode _all, mv(-4/-3=.c)

* Not included in this version of the questionnaire
mvdecode _all, mv(-5=.d)

* Version of questionnaire with modified filtering
mvdecode _all, mv(-6=.e)
*
* Question this year not part of the Survey program 
mvdecode _all, mv(-8=.f)



***************************
**  3 | Preliminary Sample Selection **
***************************

* Only private households
tab pop
keep if pop==1 | pop==2
// --> final sample selection in 3_dataprep file


*******************
** 3 | Save Data **
*******************

save "${OUT}/soepregio_ext.dta", replace

* Close log
log close
**************************************************

